package com.ruoyi.web.controller.laiqianba.product;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.product.service.IProductBaseInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.db.domain.ProductProfitInfo;
import com.ruoyi.product.service.IProductProfitInfoService;
import com.ruoyi.web.wrapper.ProductProfitInfoWrapper;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产品收益信息Controller
 * 
 * @author ruoyi
 * @date 2023-09-04
 */
@RestController
@RequestMapping("/product/profitInfo")
public class ProductProfitInfoController extends BaseController
{
    @Autowired
    private IProductProfitInfoService productProfitInfoService;
    @Autowired
    private IProductBaseInfoService productBaseInfoService;

    /**
     * 查询产品收益信息列表
     */
    @PreAuthorize("@ss.hasPermi('product:profitInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductProfitInfo productProfitInfo)
    {

        Long userId = getUserId();
        if (!(SecurityUtils.loginUserIsAdmin() || userId.equals(429L))) {
            productProfitInfo.setOwnerId(userId);
        }

        startPage();
        List<Map<String, Object>> list = productProfitInfoService.selectProductProfitInfoMapList(productProfitInfo);
        list = new ProductProfitInfoWrapper(list).wrap();
        return getDataTable(list);
    }

    /**
     * 导出产品收益信息列表
     */
    @PreAuthorize("@ss.hasPermi('product:profitInfo:export')")
    @Log(title = "产品收益信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductProfitInfo productProfitInfo)
    {
        List<ProductProfitInfo> list = productProfitInfoService.selectProductProfitInfoList(productProfitInfo);
        ExcelUtil<ProductProfitInfo> util = new ExcelUtil<ProductProfitInfo>(ProductProfitInfo.class);
        util.exportExcel(response, list, "产品收益信息数据");
    }

    /**
     * 获取产品收益信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:profitInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(productProfitInfoService.selectProductProfitInfoById(id));
    }

    /**
     * 新增产品收益信息
     */
    @PreAuthorize("@ss.hasPermi('product:profitInfo:add')")
    @Log(title = "产品收益信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductProfitInfo productProfitInfo)
    {
        return toAjax(productProfitInfoService.insertProductProfitInfo(productProfitInfo));
    }

    /**
     * 修改产品收益信息
     */
    @PreAuthorize("@ss.hasPermi('product:profitInfo:edit')")
    @Log(title = "产品收益信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductProfitInfo productProfitInfo)
    {
        return toAjax(productProfitInfoService.updateProductProfitInfo(productProfitInfo));
    }

    /**
     * 删除产品收益信息
     */
    @PreAuthorize("@ss.hasPermi('product:profitInfo:remove')")
    @Log(title = "产品收益信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(productProfitInfoService.deleteProductProfitInfoByIds(ids));
    }

    @GetMapping("/extData")
    public AjaxResult extData()
    {

        JSONObject returnData = new JSONObject();
        //获取产品名称
        returnData.put("productNameList", productBaseInfoService.productNameList());
        return AjaxResult.success(returnData);
    }

}
